

const express = require('express');

const app = express();

const superagent = require('superagent');

const cheerio = require('cheerio');

const eventproxy = require('eventproxy');

const url = require('url');

const ajax_url = 'https://cnodejs.org/';


superagent.get(ajax_url).end((err,res)=>{
	if(err) {
		console.log(err);
	}
	let tmpArr = [];

	let $ = cheerio.load(res.text);

	let $targets = $('#topic_list .topic_title');

	$targets.each((index,el)=>{
		// console.log($(el).attr('href'))
		let curr_url = $(el).attr('href');

		// 把相对路径转换成绝对路径
		 tmpArr.push(url.resolve(ajax_url,curr_url));    
	});

	// console.log(tmpArr);
	let ep = new eventproxy();

	// 命令 ep 重复监听 tmpArr.length 次 `topic_html` 事件再行动
	ep.after('topic_html',11,(toppics)=>{
		// console.log(toppics);

		let topicobj = toppics.map((item,index,arr)=>{
			// console.log(item);
			let url = item[0];
			let html = item[1];
			let $ = cheerio.load(html);
			return {
				title: $('.topic_full_title').text().trim(),
      			href: url,
      			comment1: $('.reply_content').eq(0).text().trim(),
			}
		});
		console.log(topicobj);



	});

	tmpArr.forEach((item,index)=>{
		// console.log(item);
		if(index>=11) return;
		superagent.get(item).end((err,item_res)=>{
			if(err) {
				console.log(err);
				return;
			}
			// console.log('fetch'+item +'successful');
			ep.emit('topic_html',[item,item_res.text]);
			// console.log(item_res);
		})

	})

})